<script setup lang="ts">
import { ref } from "vue";
import { userMessage } from "./utils/send";
import { PureTableBar } from "@/components/RePureTableBar";
import { useRenderIcon } from "@/components/ReIcon/src/hooks";

import Search from "@iconify-icons/ep/search";
import Refresh from "@iconify-icons/ep/refresh";
import EditPen from "@iconify-icons/ep/edit-pen";
import Delete from "@iconify-icons/ep/delete";
import AddFill from "@iconify-icons/ri/add-circle-line";

defineOptions({
  name: "Send"
});

const formRef = ref();
const {
  form,
  loading,
  columns,
  dataList,
  pagination,
  onSearch,
  resetForm,
  send,
  recall,
  addMessage,
  editMessage,
  viewMessage,
  handleDelete,
  handleSizeChange,
  handleCurrentChange
} = userMessage();
</script>

<template>
  <div class="main">
    <el-form
      ref="formRef"
      :inline="true"
      :model="form"
      class="search-form bg-bg_color w-[99/100] pl-8 pt-[12px]"
    >
      <el-form-item label="标题：" prop="title">
        <el-input
          v-model="form.title"
          placeholder="请输入标题"
          clearable
          class="!w-[200px]"
        />
      </el-form-item>
      <el-form-item label="状态：" prop="status">
        <DictOption
          v-model="form.status"
          code="Message_Send_Status"
          type="select"
          placeholder="请选择状态"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          :icon="useRenderIcon(Search)"
          :loading="loading"
          @click="onSearch"
        >
          搜索
        </el-button>
        <el-button :icon="useRenderIcon(Refresh)" @click="resetForm(formRef)">
          重置
        </el-button>
      </el-form-item>
    </el-form>

    <PureTableBar title="消息列表" :columns="columns" @refresh="onSearch">
      <template #buttons>
        <el-button
          type="primary"
          :icon="useRenderIcon(AddFill)"
          @click="addMessage()"
        >
          发送
        </el-button>
      </template>
      <template v-slot="{ size, dynamicColumns }">
        <pure-table
          align-whole="center"
          showOverflowTooltip
          table-layout="auto"
          :loading="loading"
          :size="size"
          adaptive
          :data="dataList"
          :columns="dynamicColumns"
          :pagination="pagination"
          :paginationSmall="size === 'small'"
          :header-cell-style="{
            background: 'var(--el-fill-color-light)',
            color: 'var(--el-text-color-primary)'
          }"
          @page-size-change="handleSizeChange"
          @page-current-change="handleCurrentChange"
        >
          <template #operation="{ row }">
            <el-button
              v-if="row.status == '1'"
              class="reset-margin"
              link
              type="primary"
              :size="size"
              :icon="useRenderIcon('icon-park-outline:back-one')"
              @click="recall(row.id)"
            >
              撤回
            </el-button>
            <el-button
              v-if="row.status == '2'"
              class="reset-margin"
              link
              type="primary"
              :size="size"
              :icon="useRenderIcon('mdi:email-send-outline')"
              @click="send(row.id)"
            >
              发送
            </el-button>
            <el-button
              v-if="row.status == '1'"
              class="reset-margin"
              link
              type="primary"
              :size="size"
              :icon="useRenderIcon('carbon:view')"
              @click="viewMessage(row.id)"
            >
              查看
            </el-button>
            <el-button
              v-if="row.status == '2'"
              class="reset-margin"
              link
              type="primary"
              :size="size"
              :icon="useRenderIcon(EditPen)"
              @click="editMessage(row.id)"
            >
              编辑
            </el-button>
            <el-popconfirm
              :title="`是否确认删除这条消息`"
              @confirm="handleDelete(row)"
            >
              <template #reference>
                <el-button
                  class="reset-margin"
                  link
                  type="primary"
                  :size="size"
                  :icon="useRenderIcon(Delete)"
                >
                  删除
                </el-button>
              </template>
            </el-popconfirm>
          </template>
        </pure-table>
      </template>
    </PureTableBar>
  </div>
</template>

<style scoped lang="scss">
:deep(.el-dropdown-menu__item i) {
  margin: 0;
}

.search-form {
  :deep(.el-form-item) {
    margin-bottom: 12px;
  }
}
</style>
